package administrador;
import java.util.Calendar;
import net.sf.jasperreports.engine.JRException;
/**
 *
 * @author  Jorge
 */
public class report_Inventario extends javax.swing.JFrame {
    calendario.JCalendar jCalendar = null;
    /** Creates new form report_Inventario */
    Conexion conexion;
    public report_Inventario(Conexion conexion) {
        this.conexion=conexion;
        initComponents();
        jCalendar = new calendario.JCalendar(this, "Calendario", true);
        setVisible(true);
    }
    java.sql.Date fecha1=null;
    java.sql.Date fecha2=null;
    public void sacarFecha(int opcion) {
        Calendar cDate = jCalendar.getCalendar();
        if (cDate == null) {
            cDate = Calendar.getInstance();
        }
        jCalendar.setCalendar(cDate);
        jCalendar.setVisible(true);
        if (jCalendar.isOkPressed()) {
            if (opcion == 1) {
                fecha1 = new java.sql.Date(jCalendar.getDate().getTime());
                System.out.println(fecha1);
                txt_FechaIni.setText(fecha1.toString());
            } else if (opcion == 2) {
                fecha2 = new java.sql.Date(jCalendar.getDate().getTime());
                txt_fchFin.setText(fecha2.toString());
            }
            jCalendar.setVisible(false);
        }
    }
    public void imprimirHistorialInv(String nombreArchivo){
        if(txt_FechaIni.getText().equals("")){
            javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha de Inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
        }else{
            if(txt_fchFin.getText().equals("")){
                javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha Fin","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
            }else{
                     if(txt_FechaIni.getText().compareTo(txt_fchFin.getText())<=0){
                            String path = "./Reportes/";
                            java.util.Map parametros = new java.util.HashMap();
                            parametros.put("fecha_Inicio",fecha1);
                            parametros.put("fecha_Fin",fecha2);
                            try {
                                    generaReporte(path+ nombreArchivo,parametros);
                            } catch (Exception e) {
                                    System.out.println(e.getMessage());
                            }  
                    }else{
                        javax.swing.JOptionPane.showMessageDialog(this,"La fecha fin no puede ser menor que la fecha de inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
                    }
            }
        }
    }
    public void  generaReporte(String path,java.util.Map parametros) throws JRException{
        net.sf.jasperreports.engine.JasperReport report = net.sf.jasperreports.engine.JasperCompileManager.compileReport(path);
        net.sf.jasperreports.engine.JasperPrint print = net.sf.jasperreports.engine.JasperFillManager.fillReport(report, parametros, conexion.getconexion());
        net.sf.jasperreports.view.JasperViewer.viewReport(print,false);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        jPanel1 = new javax.swing.JPanel();
        txt_FechaIni = new javax.swing.JTextField();
        txt_fchFin = new javax.swing.JTextField();
        btn_FechaInicio = new javax.swing.JButton();
        btn_FechaFin = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        jToggleButton1.setName("jToggleButton1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Historial de Inventarios"));
        jPanel1.setName("jPanel1"); // NOI18N

        txt_FechaIni.setEditable(false);
        txt_FechaIni.setName("txt_FechaIni"); // NOI18N

        txt_fchFin.setEditable(false);
        txt_fchFin.setName("txt_fchFin"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(report_Inventario.class);
        btn_FechaInicio.setIcon(resourceMap.getIcon("btn_FechaInicio.icon")); // NOI18N
        btn_FechaInicio.setName("btn_FechaInicio"); // NOI18N
        btn_FechaInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_FechaInicioActionPerformed(evt);
            }
        });

        btn_FechaFin.setIcon(resourceMap.getIcon("btn_FechaFin.icon")); // NOI18N
        btn_FechaFin.setName("btn_FechaFin"); // NOI18N
        btn_FechaFin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_FechaFinActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txt_FechaIni, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_fchFin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn_FechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_FechaFin, 0, 0, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jButton3)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_FechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_FechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_fchFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_FechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-238)/2, (screenSize.height-197)/2, 238, 197);
    }// </editor-fold>//GEN-END:initComponents

private void btn_FechaInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_FechaInicioActionPerformed
    sacarFecha(1);
}//GEN-LAST:event_btn_FechaInicioActionPerformed

private void btn_FechaFinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_FechaFinActionPerformed
    sacarFecha(2);
}//GEN-LAST:event_btn_FechaFinActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        imprimirHistorialInv("Inventario/"+"contenedor.jrxml");
}//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_FechaFin;
    private javax.swing.JButton btn_FechaInicio;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JTextField txt_FechaIni;
    private javax.swing.JTextField txt_fchFin;
    // End of variables declaration//GEN-END:variables

}
